package com.xish.api.activity.search;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class ActivitySearchConditionParser {

    public ActivitySearchCondition parse(HttpServletRequest request) {
        ActivitySearchCondition condition = new ActivitySearchCondition();

        if (request != null) {
            condition.setUuid(readRequestParam(request, "UUID", ""));
            condition.setAppKey(readRequestParam(request, "app_key", ""));
            condition.setAppVCode(readRequestParam(request, "app_v_code", ""));
            condition.setAppVName(readRequestParam(request, "app_v_name", ""));
            condition.setChannel(readRequestParam(request, "channel", ""));
            condition.setFieldsVersion(readRequestParam(request, "fields_version", ""));
            condition.setFormat(readRequestParam(request, "format", "json"));
            condition.setFree(readRequestParam(request, "free", "0"));
            condition.setGenreId(readRequestParam(request, "genre_id", "0"));
            condition.setIsNear(readRequestParam(request, "is_near", "1"));
            condition.setIsValid(readRequestParam(request, "is_valid", "1"));
            condition.setKeyword(readRequestParam(request, "keyword", ""));
            condition.setLat(readRequestParam(request, "lat", ""));
            condition.setLon(readRequestParam(request, "lon", ""));
            condition.setLimitedDiscount(readRequestParam(request, "limited_discount", ""));
            condition.setMethod(readRequestParam(request, "method", ""));
            condition.setOffset(readRequestParam(request, "offset", "0"));
            condition.setOs(readRequestParam(request, "os", ""));
            condition.setPagesize(readRequestParam(request, "pagesize", "30"));
            condition.setPoiId(readRequestParam(request, "poi_id", "0"));
            condition.setR(readRequestParam(request, "r", ""));
            condition.setTimestamp(readRequestParam(request, "timestamp", ""));
            condition.setTopicSession(readRequestParam(request, "topic_session", ""));
            condition.setV(readRequestParam(request, "v", ""));
        }

        return condition;
    }

    private String readRequestParam(HttpServletRequest request, String paramName, String defaultValue) {
        String value = request.getParameter(paramName);
        if (value == null) {
            value = defaultValue;
        }

        return value;
    }
}
